package cn.bdqn.sptparent.web.controller;


import cn.bdqn.sptparent.common.util.AESUtils;
import cn.bdqn.sptparent.domain.Result;
import cn.bdqn.sptparent.domain.param.LoginParam;
import cn.bdqn.sptparent.domain.po.User;
import cn.bdqn.sptparent.domain.vo.LoginContext;
import cn.bdqn.sptparent.service.UserService;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lhl
 * @since 2023-05-17
 */
@RestController
/*@CrossOrigin*/
@RequestMapping("/user")
@Api("用户控制类")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("/login")
    @ApiOperation(value="用户登录",notes="注意token返回")
    @ApiImplicitParams({
            @ApiImplicitParam(name="username",value="账号",required=true,paramType="json"),
            @ApiImplicitParam(name="password",value="密码",required=true,paramType="json",dataType="String"
           )
    })
    public Result<LoginContext> login(@RequestBody LoginParam loginParam, HttpServletRequest request) throws Exception {
        QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
        queryWrapper.lambda()
                .eq(User::getUserName,loginParam.getUsername())
                .eq(User::getUserPassword,loginParam.getPassword());
        LoginContext loginContext = new LoginContext(userService.getOne(queryWrapper),request);
        String token = AESUtils.encrypt(JSON.toJSONString(loginContext));// token加密

        loginContext.setToken(token);
        LoginContext u2 = JSON.parseObject(AESUtils.decrypt(token),LoginContext.class);// token解析

        // 存放 redis
        return Result.ok().data(loginContext);
    }


    @GetMapping("")
    public Result<List<User>> getAll(){
        return Result.ok().data(userService.list());
    }

    @PostMapping("")
    public Result<Boolean> add(@RequestBody User user){
        user.setUserBirthday(new Date());
        return Result.ok().data(userService.save(user));
    }

    @GetMapping("page")
    public Result<IPage<User>> page(
            @RequestParam(value = "pageNum",defaultValue = "1",required = false) Long pageNum,
            @RequestParam(value = "pageSize",defaultValue = "10",required = false) Long pageSize){
        return Result.ok().data(userService.page(new Page<User>(pageNum,pageSize)));
    }

    @GetMapping("like")
    public Result<List<User>> like(String name){
        QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
        // 查询
        queryWrapper.like("userName",name)
                .or().like("userNickName",name)
                .or().like("userRealname",name);
        return Result.ok().data(userService.list(queryWrapper));
    }

    @GetMapping("like2")
    public Result<IPage<User>> like2(
            @RequestParam(value = "pageNum",defaultValue = "1",required = false) Long pageNum,
            @RequestParam(value = "pageSize",defaultValue = "10",required = false) Long pageSize,
            String name){
        QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
        // 查询
        if(name != null){
            queryWrapper.lambda()
                    .like(User::getUserName,name)
                    .or().like(User::getUserNickName,name)
                    .or().like(User::getUserRealname,name)
                    .orderByDesc(User::getUserBirthday);
        }

        return Result.ok().data(userService.page(new Page<User>(pageNum,pageSize),queryWrapper));
    }
}
